import request from '@/utils/request';
import { ElMessage } from 'element-plus';
import { useRoute } from 'vue-router';
//后端请求域名
const functions = {
    //文件上传下载
    uploadfile: function (data) {
        return httpRequest('/index.php/app/uploadfile', 'post', data);
    },
    //用户管理
    login: function (data) {
        return httpRequest('/index.php/app/login', 'post', data, { isToken: false,language:localStorage.getItem('locale')?localStorage.getItem('locale'):'zh'  });
    },
    register: function (data) {
        return httpRequest('/index.php/app/register', 'post', data, { isToken: false,language:localStorage.getItem('locale')?localStorage.getItem('locale'):'zh'  });
    },
    logout: function (data) {
        return httpRequest('/index.php/app/logout', 'get', data);
    },
    getUserList: function (data) {
        return httpRequest('/index.php/app/user/find', 'post', data);
    },
    changeUserInfo: function (data) {
        return httpRequest('/index.php/app/user/save', 'post', data);
    },
    deleteUserInfo: function (data) {
        return httpRequest('/index.php/app/user/delete', 'post', data);
    },
    Setchannel: function (data) {
        return httpRequest('/index.php/app/user/Setchannel', 'post', data);
    },
    changePassword: function (data) {
        return httpRequest('/index.php/app/user/changePassword', 'post', data);
    },
    //人事档案
    getemployeelist: function (data) {
        return httpRequest('/index.php/app/employee/find', 'post', data);
    },
    getemployeedetail: function (data) {
        return httpRequest('/index.php/app/employee/finddetail', 'post', data);
    },
    saveemployee: function (data) {
        return httpRequest('/index.php/app/employee/save', 'post', data);
    },
    deleteemployee: function (data) {
        return httpRequest('/index.php/app/employee/delete', 'post', data);
    },
    excelimportemployee: function (data) {
        return httpRequest('/index.php/app/employee/excelimport', 'post', data);
    },
    //部门档案
    getdeptlist: function (data) {
        return httpRequest('/index.php/app/dept/find', 'get', data);
    },
    savedept: function (data) {
        return httpRequest('/index.php/app/dept/save', 'post', data);
    },
    deletedept: function (data) {
        return httpRequest('/index.php/app/dept/delete', 'post', data);
    },
    //组织机构
    getchannellist: function (data) {
        return httpRequest('/index.php/app/channel/find', 'get', data);
    },
    savechannel: function (data) {
        return httpRequest('/index.php/app/channel/save', 'post', data);
    },
    deletechannel: function (data) {
        return httpRequest('/index.php/app/channel/delete', 'post', data);
    },
    loadchannel: function (data) {
        return httpRequest('/index.php/app/channel/loadchannel', 'get', data);
    },
    //角色管理
    getUsrGroup: function (data) {
        return httpRequest('/index.php/app/UsrGroup/find', 'post', data);
    },
    saveUsrGroup: function (data) {
        return httpRequest('/index.php/app/UsrGroup/save', 'post', data);
    },
    deleteUsrGroup: function (data) {
        return httpRequest('/index.php/app/UsrGroup/delete', 'post', data);
    },
    excelimportUsrGroup: function (data) {
        return httpRequest('/index.php/app/UsrGroup/excelimport', 'post', data);
    },
    //自定义配置
    getComUiSet: function (data) {
        return httpRequest('/index.php/app/ComUiSet/find', 'post', data);
    },
    saveComUiSet: function (data) {
        return httpRequest('/index.php/app/ComUiSet/save', 'post', data);
    },
    deleteComUiSet: function (data) {
        return httpRequest('/index.php/app/ComUiSet/delete', 'post', data);
    },
    //用户审批查询
    getuserapply: function (data) {
        return httpRequest('/index.php/app/Userapply/find', 'post', data);
    },
    //用户审批
    userapplyhandle: function (data) {
        return httpRequest('/index.php/app/Userapply/handle', 'post', data);
    },
    //用户审批删除
    userapplydelete: function (data) {
        return httpRequest('/index.php/app/Userapply/delete', 'post', data);
    },
    //登录日志
    getUsrlogin: function (data) {
        return httpRequest('/index.php/app/Logfile/find', 'post', data);
    },
    //操作日志
    getoperatelog: function (data) {
        return httpRequest('/index.php/app/UseLog/find', 'post', data);
    },
    //自定义菜单
    LoadMenu: function (data) {
        return httpRequest('/index.php/app/CustomMenu/LoadMenu', 'get', data);
    },
    Menufindlist: function (data) {
        return httpRequest('/index.php/app/CustomMenu/findlist', 'post', data);
    },
    Menufinddetail: function (data) {
        return httpRequest('/index.php/app/CustomMenu/finddetail', 'post', data);
    },
    Menusave: function (data) {
        return httpRequest('/index.php/app/CustomMenu/save', 'post', data);
    },
    Menudelete: function (data) {
        return httpRequest('/index.php/app/CustomMenu/delete', 'post', data);
    },
    Menuenablehandle: function (data) {
        return httpRequest('/index.php/app/CustomMenu/enablehandle', 'post', data);
    },
    //权限管理
    Grantfindlist: function (data) {
        return httpRequest('/index.php/app/Grantfunction/findlist', 'post', data);
    },
    Grantsave: function (data) {
        return httpRequest('/index.php/app/Grantfunction/save', 'post', data);
    },
    loadgrant: function (data) {
        return httpRequest('/index.php/app/Grantfunction/loadgrant', 'get', data);
    },
    //其它功能测试
    //redis队列任务分发
    missiondispatch: function (data) {
        return httpRequest('/index.php/app/missiondispatch', 'get', data);
    },
    //契约测试
    pay: function (data) {
        return httpRequest('/index.php/app/pay', 'get', data);
    },
    //大数据测试
    getTesttab: function (data) {
        return httpRequest('/index.php/app/Testtab/find', 'post', data);
    },
    saveTesttab: function (data) {
        return httpRequest('/index.php/app/Testtab/save', 'post', data);
    },
    deleteTesttab: function (data) {
        return httpRequest('/index.php/app/Testtab/delete', 'post', data);
    },
    generate: function (data) {
        return httpRequest('/index.php/app/Testtab/generate', 'post', data);
    },

    
};
function httpRequest(url, method, data, headers = { isToken: true,language:localStorage.getItem('locale')?localStorage.getItem('locale'):'zh' }) {
    return request({
        url: url,
        headers: headers,
        method: method,
        data: data,
    });
}

export function httpSend(funcName, args, isnotice = true) {
    const func = functions[funcName];
    if (typeof func === 'function') {
        return func(args).then(
            (res) => {
                if (Number(res.code) === -1) {
                    localStorage.setItem('loginout', true);
                    if (window.location.pathname !== '/web/login') {
                        window.location.href = '/web/login';
                    }
                    console.log('登录过期');
                }
                if (Number(res.code) === 1) {
                    console.log('请求成功 ');
                    if (isnotice) {
                        ElMessage({ message: res.msg, type: 'success' });
                    }
                }
                if (Number(res.code) === 0) {
                    ElMessage.error(res.msg);
                    console.log('请求失败');
                }
                return res;
            },
            (err) => {
                console.log('err', err);
            },
            () => {
                // this.msgError(this.$t("i18n.loginFormHint4"));
                console.log('done');
            }
        );
    } else {
        console.log('Function not found:', funcName);
        return null;
    }
}
